package com.myjdbc.demo;


import java.sql.SQLException;
import java.util.List;
import com.myjdbc.demo.Account;
import com.myjdbc.demo.BeanHandler;
import com.myjdbc.demo.BeanListHandler;
import com.myjdbc.demo.MyJdbcUtils;

public class AccountDao {

    public void add(Account account) throws SQLException{
        String sql = "insert into account(name,money) values(?,?)";
        Object params[] = {account.getName(),account.getMoney()};
        MyJdbcUtils.update(sql, params);
    }
    
    
    public void delete(int id) throws SQLException{
        String sql = "delete from account where id=?";
        Object params[] = {id};
        MyJdbcUtils.update(sql, params);
    }
    
    public void update(Account account) throws SQLException{
        
        String sql = "update account set name=?,money=? where id=?";
        Object params[] = {account.getName(),account.getMoney(),account.getId()};
        MyJdbcUtils.update(sql, params);
        
    }
    
    public Account find(int id) throws SQLException{
        String sql = "select * from account where id=?";
        Object params[] = {id};
        return (Account) MyJdbcUtils.query(sql, params, new BeanHandler(Account.class));
    }
    
    public List<Account> getAll() throws SQLException{
        String sql = "select * from account";
        Object params[] = {};
        return (List<Account>) MyJdbcUtils.query(sql, params,new BeanListHandler(Account.class));
    }
}